package com.lanswon.qzsmk.util.pdfutil;

import java.io.File;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Logger;

import com.lanswon.qzsmk.base.PathContants;
import com.lanswon.qzsmk.util.QRCodeUtil;

/**
 * 描述：生成申请表单打印文件 创建时间：2018/02/12 版本：v1.0
 * 
 * @author 惠国磊
 *
 */
public class TemplateAppRegPDF {

	/* logger对象 */
	private final static Logger logger = (Logger) LoggerFactory.getLogger(TemplateAppRegPDF.class);

	public void createApplicationTicket(Map<String,String> workMap,HttpServletRequest request){
		logger.info("createApplicationTicket########start()");
		
		/*1.模板文件路径(后期放在服务器公共路径下)*/
	//	String tempPath = "D:\\template\\";
		String tempPath = "../../pdf_template/";
		/* 2.模板（文件）名称 */
		String tempFileName = "applicationRegister.pdf";
		/* 3.目标文件路径 */
	//	String targetPath = "smb://commonuser:Lanswon2018@192.168.0.12/qzsmk/00.temple/";
		//需要再服务器端先生成，然后download到本地,提供打印，提供上传。
		//即真正上传的时候，逻辑只要判断服务器端的名字与文件是否一致就行，由服务器端转移
		//或者本地上传到服务器
		String fileDir = workMap.get("paperNo");
		logger.debug("文件夹名称是==>"+fileDir);
//		String targetPath = "D:\\Apache24\\htdocs\\qzsmk\\resource\\temple\\"+fileDir+"\\";
//		String targetPath = request.getSession().getServletContext().getRealPath("/uploadImages/enclosure/")+File.separator+fileDir;

		//惠国磊 20180927 修改targetPath 暂时将打印单放到54服务器上，后期再改
		HttpSession session =  request.getSession();
		String root =session.getServletContext().getRealPath("/");
//		String targetPath = "/home/qzsmk/resource/credentials/collections"+File.separator+fileDir;
		
		String targetPath = root + PathContants.PROJECT_PATH_ENCLOSURE_IMPORTED+File.separator+fileDir;
		File dirFile = new File(targetPath);
		if(!dirFile.exists()){
			dirFile.mkdirs();
		}
		/* 4.目标文件名称 */
		String targetFileName = null;
		
		//以下是测试代码
		String pdfName = workMap.get("pdfName");
		if("".equals(pdfName) || null == pdfName){
			logger.error("！！！！！！！！！！！");
		}
		targetFileName = pdfName+"_applicationRegister.pdf";
		if(logger.isDebugEnabled()){
			logger.debug("targetFileName:"+targetFileName);
		}
		ApplicationRegisterTicket appRegTt = new ApplicationRegisterTicket();
		//设置采集网点（画面传过来）
		appRegTt.setCollectionpoint(workMap.get("collectionpoint"));
		//设置采集单号（自动生成，另外写util方法）
		appRegTt.setCollectionNo(workMap.get("collectionNo"));
		
		//设置员工号（画面传过来）
		appRegTt.setEmployeeID(workMap.get("employeeID"));
		//设置卡代码（画面传值）
		appRegTt.setCardCode(workMap.get("cardpName"));
		//设置卡类型(画面传值)
		appRegTt.setCardTypeCode(workMap.get("cardtype"));
		//设置性别(画面传值)
		appRegTt.setGender(workMap.get("gender"));
		//customerPhoto
		appRegTt.setCustomerName(workMap.get("customerName"));
		//设置证件类型名称（画面传值）
		appRegTt.setPaperType(workMap.get("papertype"));
		appRegTt.setPaperNo(workMap.get("paperNo"));
		//设置省市（画面传值）
		appRegTt.setPrAddrProvince(workMap.get("prAddrProvince"));
		//设置城市(画面传值)
		appRegTt.setPrAddrCity(workMap.get("prAddrCity"));
		//设置联系地址省市区详细地址(画面传值)
		appRegTt.setContactAddrProvince(workMap.get("contactAddrProvince"));
		appRegTt.setContactAddrCity(workMap.get("contactAddrCity"));
		appRegTt.setContactAddrCounty(workMap.get("contactAddrCounty"));
		appRegTt.setContactAddrStreet(workMap.get("contactAddrStreet"));
		appRegTt.setContactAddrDetailedInfo(workMap.get("contactAddrDetailedInfo"));
		//设置联系电话(画面传值)
		appRegTt.setMobile(workMap.get("mobile"));
		//设置生日
		appRegTt.setBirth(workMap.get("birth"));
		
		//现居住地的设置
		appRegTt.setCurAddrProvince(workMap.get("curAddrProvince"));
		appRegTt.setCurAddrCity(workMap.get("curAddrCity"));
		appRegTt.setCurAddrCounty(workMap.get("curAddrCounty"));
		appRegTt.setCurAddrStreet(workMap.get("curAddrStreet"));
		appRegTt.setCurAddrDetailInfo(workMap.get("curAddrDetailedInfo"));
		
		//设置邮政编码
		appRegTt.setCurPostalcode(workMap.get("contactPostalcode"));
		appRegTt.setContactPostalcode(workMap.get("contactPostalcode"));
		//设置民族		
		appRegTt.setNationality(workMap.get("nationality"));
		appRegTt.setEmail(workMap.get("email"));
		appRegTt.setHomePhone(workMap.get("homePhone"));
		appRegTt.setDate(workMap.get("date"));
		
		PDFApplicationRegTicket pdfAppTT = new PDFApplicationRegTicket();
		
		// 指定服务器路径，pdf文件的名称定义为每个流程凭证+是市民卡ha
		pdfAppTT.setTemplatePdfPath(tempPath+tempFileName);
		
		// 打印机上的服务
		pdfAppTT.setTargetPdfpath(targetPath+File.separator+targetFileName);
//		String root=request.getSession().getServletContext().getContextPath();
//		request.getSession().setAttribute("pdfPath", root+File.separator+"uploadImages"+File.separator+"enclosure"+File.separator+workMap.get("paperNo")+File.separator+targetFileName);
		//惠国磊 20180927 以下一行 代替以上两行
		session.setAttribute("pdfPath",session.getServletContext().getContextPath()+File.separator + PathContants.PROJECT_PATH_ENCLOSURE_IMPORTED +File.separator+ workMap.get("paperNo")+File.separator+targetFileName);
		logger.debug("pdfTT_path:"+pdfAppTT.getTargetPdfpath());
		pdfAppTT.setAppticket(appRegTt);
		
		
		//生成二维码  并上传
		String qRImgPath = root + PathContants.PROJECT_PATH_ENCLOSURE_IMPORTED+File.separator+fileDir;
				
//		request.getSession().getServletContext().getRealPath("/uploadImages/enclosure/")+File.separator+workMap.get("paperNo");

		//返回二维码的位置
		String ppp = QRCodeUtil.encode(workMap.get("collectionNo"),qRImgPath+File.separator+pdfName+"_QR.png",true);//没有内嵌的logo

		pdfAppTT.setQRCodeImgPath(ppp);

		
		String imgPath = root + PathContants.PROJECT_PATH_IMG_IMPORTED;
//		String imgPath = request.getSession().getServletContext().getRealPath("/uploadImages/photos/");
				

		//20180619 惠国磊
		//  设置图片路径
		//    获取图片的名称
		File ff = new File(imgPath);
		if(!ff.exists()){
			ff.mkdirs();
		}
		File ff1 = new File(qRImgPath);
		if(!ff1.exists()){
			ff1.mkdirs();
		}
		
		
		String	imageObjPath ="";
		boolean b = false;
		for(File fff:ff.listFiles()){
			if(fff.getName().contains(workMap.get("paperNo"))){
					imageObjPath =  imgPath +File.separator+fff.getName();
		b = true;
		logger.debug("图片的路径====>"+imageObjPath);
		break;
			}}
		
		if(!b){
			imageObjPath =  root +File.separator+"themes"+File.separator+"default"+File.separator+"images"+File.separator+"default.png";
		}
		
		
		pdfAppTT.setImagePath(imageObjPath);
		//创建远程pdf文件
		File file = new File(targetPath+File.separator+targetFileName);
		if(file.exists()){
			file.delete();
		}
	//	file.connect();
		pdfAppTT.tempAppReg(file);
		logger.info("createApplicationTicket########end()");
		
		
	}
}
